package view.employee;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dal.database.DBUtil;
import dal.employee.Employee;
import dal.employee.EmployeeUtil;

/**
 * Servlet implementation class AddEmployee
 */
@WebServlet("/AddEmployee")
public class AddEmployee extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddEmployee() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		/*
		 * Get all required info from EmployeeAdd.jsp
		 * Create Employee class instance
		 * set corresponding info into that instance
		 * Create add(Object obj) of EmployeeUtil class
		 * 
		 * also handle all poossible exception
		 */
		PrintWriter printWriter=response.getWriter();
		response.setContentType("text/HTML");
		try {
			
			java.util.Date d=new java.util.Date();
			DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
			String date=df.format(d);
			System.out.println(date);
			
			String dt[]=date.split("-");
						
			Date Date_Added=new Date(Integer.parseInt(dt[0]),Integer.parseInt(dt[1]), Integer.parseInt(dt[2]));
			
			String Name=request.getParameter("Name");
			String Current_Address=request.getParameter("CurrentAddress");
			String Permnt_Address=request.getParameter("PermntAddress");
			String Mobile=request.getParameter("Mobile");
			String Email_id=request.getParameter("EmailId");
			String Alternate_Email_id=request.getParameter("AlternateEmailId");
			String Designation=request.getParameter("Designation");
			
			
			Employee employee=new Employee();
			EmployeeUtil employeeUtil=new EmployeeUtil();
			
			String employeeId=employeeUtil.getId();
			System.out.println(employeeId);
			employee.setAlternateEmailId(Alternate_Email_id);
			employee.setCurrentAddress(Current_Address);
			employee.setPermenentAddress(Permnt_Address);
			employee.setDateAdded(Date_Added);
			employee.setDesignation(Designation);
			employee.setEmailId(Email_id);
			employee.setEmployeeId(employeeId);
			employee.setMobile(Mobile);
			employee.setName(Name);
			
			
			Boolean isOk=employeeUtil.add(employee);
			
			if(isOk=true)
			{
				printWriter.print(""
						+ "<b>"
						+ " Employee Information Added Succesfully "
						+ "</b>");
			}
			else
			{
				printWriter.print(""
						+ "<b>"
						+ " Employee Information Not Added Succesfully "
						+ "</b>");
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Exception from post() AdddEmployee");
		}
		
	}

}
